package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.ExportExcel;
import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.OperatorMapper;
import com.lucher.mes.dao.TooldepotMapper;
import com.lucher.mes.dao.ToolinoutMapper;
import com.lucher.mes.dao.ToolplateMapper;
import com.lucher.mes.po.Toolinout;
import com.lucher.mes.po.Toolplate;
import com.lucher.mes.po.Operator;
import com.lucher.mes.po.Tooldepot;
import com.lucher.mes.service.IOperatorService;

/**
 * 用户控制层
 */
@Controller
@Scope("prototype")
@RequestMapping("/toolinout")
@Transactional
public class ToolinoutController extends BaseController {

	@Autowired
	private ToolinoutMapper toolinoutDao;
	@Autowired
	private IOperatorService operatorService;
	@Autowired
	private ToolplateMapper toolplateDao;
	@Autowired
	private OperatorMapper operatorDao;
	
	/*
     * 特殊的instance变量
     * 注：零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码：
     * 生成零长度的byte[]对象只需3条操作码，而Object lock = new Object()则需要7行操作码。
     */
    private static byte[] lock = new byte[0];
	
	/**
	 * 查询材料库存
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Toolinout form) {
		return "toolinout/findToolinoutList";
	}
	
	/**
	 * 查询材料库存Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Toolinout form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		if(StringUtils.isNotBlank(form.getRangeDate())){
			String[] datsArray = form.getRangeDate().split(" - ");
			if(datsArray!=null && datsArray.length == 2){
				form.setBegintime(datsArray[0]);
				form.setEndtime(datsArray[1]);
			}
		}
		
		Integer total = toolinoutDao.findByCount(form);
		List<Toolinout> list = toolinoutDao.findByListForPage(form);
		for (Toolinout toolinout : list) {
			
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("id", toolinout.getId());
			objectMap.put("toolcode", toolinout.getToolcode());
			objectMap.put("toolname", toolinout.getToolname());
			objectMap.put("model", toolinout.getModel());
			objectMap.put("description", toolinout.getDescription());
			objectMap.put("operatorcode", toolinout.getOperatorcode());
			objectMap.put("inoutamount", toolinout.getInoutamount());
			objectMap.put("usetime", StringUtils.isEmpty(toolinout.getUsetime())?"":Tools.getStr(toolinout.getUsetime()).substring(0, 19));
			objectMap.put("useinfo", toolinout.getUseinfo());
			objectMap.put("inoutercode", toolinout.getInoutercode());
			objectMap.put("inoutername", toolinout.getInoutername());
			objectMap.put("inoutstatus", toolinout.getInoutstatus());
			objectMap.put("depotrackcode", toolinout.getDepotrackcode());
			objectMap.put("returntime", StringUtils.isEmpty(toolinout.getReturntime())?"":Tools.getStr(toolinout.getReturntime()).substring(0, 19));
			objectMap.put("returninfo", toolinout.getReturninfo());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);//页面总数
		result.put("data", objectlist);
		return result;
	}
	
	/**
	 * 单个删除
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/delete")
	public Map<String,Object> delete(Toolinout form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		
		//删除产品
		toolinoutDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "删除成功");
		
		return responseJson;
	}
	
	/**
	 * 工具归还
	 */
	@RequestMapping(value = "/returndepot")
	public String returndepot(Toolinout form,Model model) {
		Toolinout toolinout = toolinoutDao.selectByPrimaryKey(form.getId());
		toolinout.setUsetime(StringUtils.isEmpty(toolinout.getUsetime())?"":Tools.getStr(toolinout.getUsetime()).substring(0, 19));
		form.setToolinout(toolinout);
		model.addAttribute("operatorlist", operatorDao.findOperatorList(new Operator()));
		return "toolinout/returndepot";
	}
	
	/**
	 * 保存工具归还
	 */
	@RequestMapping(value = "/saveOutdepot")
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	public  Map<String,Object>  saveOutdepot(Toolinout form) {
		Operator operator = (Operator)getSession().getAttribute("Operator");
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		//加锁
		synchronized(lock) {
			//查询工具库 存不存在此种产品
			Toolinout inout = toolinoutDao.selectByPrimaryKey(form.getId());
			if(inout != null){//没有领用记录
				if("领用".equals(inout.getInoutstatus())){
					inout.setInoutstatus("归还"); //无库存
					inout.setReturntime(Tools.getCurrentTime());//归还时间
					toolinoutDao.updateByPrimaryKeySelective(inout);
					//增加工具库存
					Toolplate toolplate = toolplateDao.findByToolcode(form.getToolcode());
					toolplate.setLeftnum(toolplate.getLeftnum() + inout.getInoutamount());
					toolplate.setOutnum(toolplate.getOutnum()-inout.getInoutamount());
					toolplateDao.updateByPrimaryKeySelective(toolplate);
					
					responseJson.put("flag", "1"); 
					responseJson.put("msg", "操作成功");
					return responseJson;
				}else{ 
					responseJson.put("flag", "0"); //操作失败
					responseJson.put("msg", "操作失败，此工具不是领用状态！");
					return responseJson;
				}
			}else{
				responseJson.put("flag", "0"); //操作失败
				responseJson.put("msg", "操作失败，此工具没有领用记录！");
				return responseJson;
			}
		}
	}
}
